<?php


namespace app\admin\controller\comment;


use app\common\controller\Adminbase;
use think\Db;
use function Symfony\Component\String\b;

class Comment extends Adminbase
{
    public function index()
    {
        if ($this->request->isAjax()) {
            $where[] = ['c.is_del', '=', 0];
            list($page, $limit) = $this->buildTableParames();
            $filter = input('filter');
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['id'])) {
                    $where[] = ['c.id', '=', $filter['id']];
                }
                if (isset($filter['order_no'])) {
                    $where[] = ['c.order_no', 'like', '%' . $filter['order_no'] . '%'];
                }
                if (isset($filter['nickname'])) {
                    $where[] = ['m.nickname', 'like', '%' . $filter['nickname'] . '%'];
                }
                if (isset($filter['type'])) {
                    $where[] = ['c.type', '=', $filter['type']];
                }
                if (isset($filter['status'])) {
                    $where[] = ['c.status', '=', $filter['status']];
                }
            }
            $_list = Db::name('comment')
                ->alias('c')
                ->join('member m','c.member_id=m.id')
                ->field('c.*,m.nickname,m.avatarUrl,m.mobile')
                ->where($where)
                ->page($page, $limit)
                ->order('create_time desc')
                ->select();
            $total = Db::name('comment')
                ->alias('c')
                ->join('member m','c.member_id=m.id')
                ->field('c.*,m.nickname,m.avatarUrl,m.mobile')
                ->where($where)
                ->count();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                if ($v['type']==3){
                   $people_info= Db::name('driver')->find($v['driver_id']);
                    $_list[$k]['people_name']=$people_info['name'];
                    $_list[$k]['phone']=$people_info['phone'];
                }else{
                    $people_info= Db::name('pz')->find($v['pz_id']);
                    $_list[$k]['people_name']=$people_info['name'];
                    $_list[$k]['phone']=$people_info['phone'];
                }
            }
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        return $this->fetch();
    }

    public function replyList(){
        $id = $this->request->param('id/d', 0);
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $data['comment_id']=$id;
            $reply_comment = Db::name('comment')->where([
                'is_del' => 0,
                'reply_id' => $data['comment_id']
            ])->order('create_time desc')
                ->page($page, $limit)
                ->select();
            $total = Db::name('comment')->where([
                'is_del' => 0,
                'reply_id' => $data['comment_id']
            ])->count();
            foreach ($reply_comment as $k => $v) {
                $reply_comment[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                if ($v['reply_role'] == 1) {
                    $member_info = Db::name('member')->find($v['member_id']);
                    $reply_comment[$k]['user_name'] = $member_info['nickname'];
                    $reply_comment[$k]['user_avatar_url'] = $member_info['avatarUrl'];
                } else if ($v['reply_role'] == 2) {
                    $business_info = Db::name('business')->find($v['business_id']);
                    $reply_comment[$k]['user_name'] = '商家:'.$business_info['business_name'];
                    $reply_comment[$k]['user_avatar_url'] = $business_info['logo_url'];
                } else if ($v['reply_role'] == 3) {
                    $reply_comment[$k]['user_name'] = '平台:惊喜食光';
                    $reply_comment[$k]['user_avatar_url'] = config('site.web_sit_avatar');
                }
            }
            $_list=$reply_comment;
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        $this->assign([
            'id' => $id,
        ]);
        return $this->fetch();
    }

    //
    public function commentList()
    {
        //创建时间 商家商品
        $id = $this->request->param('id/d', 0);
        if ($this->request->isAjax()) {
            $where[] = ['co.reply_id', '=', 0];
            $where[] = ['co.is_del', '=', 0];
            $where[] = ['co.business_id', '=', $id];
            list($page, $limit) = $this->buildTableParames();
            $filter = input('filter');
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['id'])) {
                    $where[] = ['co.id', '=', $filter['id']];
                }
                if (isset($filter['status'])) {
                    $where[] = ['co.status', '=', $filter['status']];
                }
                if (isset($filter['create_time'])) {
                    $create_time = explode(' - ', $filter['create_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['co.create_time', 'between', [$create_time[0], $create_time[1]]];
                }
                if (isset($filter['nickname'])) {
                    $where[] = ['m.nickname', 'like', '%' . $filter['nickname'] . '%'];
                }
                if (isset($filter['goods_name'])) {
                    $where[] = ['g.name', 'like', '%' . $filter['goods_name'] . '%'];
                }
                if (isset($filter['order_no'])) {
                    $where[] = ['o.order_no', 'like', '%' . $filter['order_no'] . '%'];
                }
            }
            $_list = Db::name('comment')->alias('co')
                ->join('goods g', 'co.goods_id=g.id')
                ->join('member m', 'co.member_id=m.id')
                ->join('order o', 'co.order_id=o.id')
                ->where($where)
                ->field('co.*,m.nickname,m.mobile,g.name as goods_name,o.order_no')
                ->page($page, $limit)
                ->order('co.create_time desc')
                ->select();
            $total = Db::name('comment')->alias('co')
                ->join('goods g', 'co.goods_id=g.id')
                ->join('member m', 'co.member_id=m.id')
                ->join('order o', 'co.order_id=o.id')
                ->where($where)
                ->count();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
            }
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        //经营方式
        $operate_type = Db::name('goods')->where([
            'business_id' => $id,
            'is_del' => 0
        ])->order('create_time desc')->select();
        foreach ($operate_type as $k => $v) {
            $goods_list2[$v['id']] = $v['name'];
        }
        $this->assign([
            'business_id' => $id,
            'goods_list' => $goods_list2
        ]);
        return $this->fetch();
    }

    public function replay()
    {
        if ($this->request->isPost()) {
            $data = $this->request->Post();
            $insert_data = array(
                'content' => $data['reply_content'],
                'create_time' => time(),
                'reply_id' => $data['id'],
                'reply_role'=>3
            );
            $comment_id = Db::name('comment')->insertGetId($insert_data);
            if ($comment_id) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        }
        $id = $this->request->param('id/d', 0);
        //获取评价消息
        //获取相关图片
        $data = Db::name('comment')
            ->alias('c')
            ->join('goods g','g.id=c.goods_id')
            ->field('c.*,g.name as goods_name,g.img as goods_img')
            ->find($id);
        $data['img_list'] = Db::name('comment_img')
            ->where('comment_id', '=', $id)
            ->select();
        $return=Db::name('comment')->where(['reply_id'=>$data['id'],'reply_role'=>3])->order('create_time desc')->find();
        $return_content=$return['content'];
        $this->assign([
            'data' => $data,
            'return_content'=>$return_content
        ]);
        return $this->fetch();
    }

    public function setstate()
    {
        $id = $this->request->param('id/d', 0);

        $status = $this->request->param('value/d');


        if (Db::name('comment')->where('id', $id)->update(['status' => $status])) {

            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }


    public function getc_cate()
    {
        $id = input('id');
        $query = db('area')->where('pid', $id)->select();
        return json(['code' => 200, 'data' => $query, 'msg' => '获取成功']);
    }


    public function del()
    {

        $ids = $this->request->param('ids/a', null);

        try {
            foreach ($ids as $id) {
                Db::name('comment')->where('id', $id)->update([
                    'is_del' => 1,
                ]);
            }
        } catch (\Exception $ex) {
            $this->error($ex->getMessage());
        }

        $this->success('删除成功！');
    }

    public function edit(){
        if ($this->request->isPost()) {

        }
        $id = $this->request->param('id/d', 0);
        $data = Db::name('comment')->find($id);
        $this->assign('data', $data);
        return $this->fetch();
    }

}